<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 3/29 0029
 * Time: 16:13
 */

namespace app\index\model;


use think\Model;
use think\Session;

class Manager extends Model
{
    /**
     * 增加管理员
     * @return false|int
     */
    public function addmanager(){
        $res=$this->save([
            "nickname"=>request()->param("nickname"),
            "username"=>request()->param("username"),
            "password"=>md5(request()->param("password")),
            "authlist"=>array_key_exists("auth",request()->post())?implode(",",request()->post()["auth"]):"",
            "merchantid"=>request()->param("merchantid"),
            "type"=>1
        ]);
        return $res;
    }

    /**
     * 修改管理员
     */
    public function editmanager(){
        $data['username']=request()->param("username");
        if(request()->post("password")){
            $data['password']=md5(request()->param("password"));
        }
        $data['authlist']=array_key_exists("auth",request()->post())?implode(",",request()->post()["auth"]):"";
        $res=$this->save($data,["id"=>request()->param("managerid")]);
        return $res;
    }

    /**
     * 获取管理员列表
     */
    public function getall($merchantid){
        return $this->where(["merchantid"=>$merchantid,"type"=>1])->select();
    }

    /**
     * 获取管理员一个
     */
    public function getone($managerid){
        return $this->where(["id"=>$managerid,"type"=>1])->find();
    }

    /**
     * 增加收银员
     * @return false|int
     */
    public function addcashier(){
        $res=$this->save([
            "nickname"=>request()->param("nickname"),
            "username"=>request()->param("username"),
            "password"=>md5(request()->param("password")),
            "authlist"=>array_key_exists("auth",request()->post())?implode(",",request()->post()["auth"]):"",
            "shopid"=>request()->param("shopid"),
            "type"=>2
        ]);
        return $res;
    }

    /**
     * 修改收银员
     */
    public function editcashier(){
        $data['username']=request()->param("username");
        if(request()->post("password")){
            $data['password']=md5(request()->param("password"));
        }
        $data['authlist']=array_key_exists("auth",request()->post())?implode(",",request()->post()["auth"]):"";
        $res=$this->save($data,["id"=>request()->param("managerid")]);
        return $res;
    }

    /**
     * 获取收银员列表
     */
    public function getallcashier($shopid){
        return $this->where(["shopid"=>$shopid,"type"=>2])->select();
    }

    /**
     * 获取收银员一个
     */
    public function getonecashier($managerid){
        return $this->where(["id"=>$managerid,"type"=>2])->find();
    }

    /**
     * 检查登录
     */
    public function checklogin(){
        $res=$this->where(["username"=>request()->post("username")])->find();
        if($res){
            if($res["password"]==md5(request()->post("password"))){
                Session::set("managerid",$res["id"]);//设置managerid
                Session::set("usertype",$res["type"]);
                if($res["type"]==1){
                    Session::set("merchantid",$res["merchantid"]);
                }else{
                    Session::set("shopid",$res["shopid"]);
                    Session::set("shopname",\model("shop")->getname($res["shopid"]));
                }
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * 根据用户ID获得当前的merchantid
     */
    public function getmerchantid($managerid){
        $res=$this->where('id',$managerid)->find();
        if($res["type"]==2){
            return \model("shop")->where("id",$res["shopid"])->column("merchantid")[0];
        }else{
            return $res['merchantid'];
        }
    }

}